In [5]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Define the cost function
def cost_function(x, y, z):
    return x**2 + y**2 + z**2

# Define the gradient of the cost function
def gradient(x, y, z):
    grad_x = 2 * x
    grad_y = 2 * y
    grad_z = 2 * z
    return grad_x, grad_y, grad_z

# Initialize starting point and hyperparameters
x, y, z = 5.0, 5.0, 5.0
learning_rate = 0.1
num_iterations = 100

# Lists to store the trajectory
x_trajectory, y_trajectory, z_trajectory = [], [], []

# Gradient Descent
for i in range(num_iterations):
    grad_x, grad_y, grad_z = gradient(x, y, z)
    x -= learning_rate * grad_x
    y -= learning_rate * grad_y
    z -= learning_rate * grad_z
    
    x_trajectory.append(x)
    y_trajectory.append(y)
    z_trajectory.append(z)

# Create a 3D meshgrid for the cost function surface
x_range = np.linspace(-6, 6, 100)
y_range = np.linspace(-6, 6, 100)
z_range = np.linspace(-6, 6, 100)
X, Y, Z = np.meshgrid(x_range, y_range, z_range)
cost_surface = cost_function(X, Y, Z)

# Create a 3D scatter plot of the trajectory with a color gradient
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot the cost function surface as a wireframe
ax.plot_wireframe(X, Y, cost_surface, cmap='viridis', alpha=0.3)

# Create a color gradient for the trajectory
colors = np.arange(len(x_trajectory))

# Plot the trajectory of gradient descent with a color gradient
scatter = ax.scatter(x_trajectory, y_trajectory, z_trajectory, c=colors, cmap='cool', marker='o', s=50)
ax.scatter([0], [0], [0], color='green', s=100, label='Optimal Solution')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Cost Function')
ax.set_title('Gradient Descent in 3D')
plt.legend()
plt.colorbar(scatter, label='Iteration')
plt.show()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[5], line 47
     44 ax = fig.add_subplot(111, projection='3d')
     46 # Plot the cost function surface as a wireframe
---> 47 ax.plot_wireframe(X, Y, cost_surface, cmap='viridis', alpha=0.3)
     49 # Create a color gradient for the trajectory
     50 colors = np.arange(len(x_trajectory))

File C:\ProgramData\anaconda3\Lib\site-packages\mpl_toolkits\mplot3d\axes3d.py:1732, in Axes3D.plot_wireframe(self, X, Y, Z, **kwargs)
   1730 had_data = self.has_data()
   1731 if Z.ndim != 2:
-> 1732     raise ValueError("Argument Z must be 2-dimensional.")
   1733 # FIXME: Support masked arrays
   1734 X, Y, Z = np.broadcast_arrays(X, Y, Z)

ValueError: Argument Z must be 2-dimensional.
In [6]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Define a quadratic function
def mk_quad(a, b):
    x = np.linspace(0, 4, 50)
    return a * x**2 + b

# Define the Mean Absolute Error (MAE) function
def mae_np(pred, target):
    return np.mean(np.abs(pred - target))

# Define the loss function
def loss(a, b):
    f = mk_quad(a, b)
    return mae_np(f, y)

# Create a grid of parameter values
grid_x = np.linspace(0, 4, 50)
grid_y = np.linspace(0, 4, 50)
X, Y = np.meshgrid(grid_x, grid_y)

# Calculate the loss for each parameter combination
Z = np.zeros((50, 50))
for x1 in range(len(X)):
    for y1 in range(len(Y)):
        Z[x1][y1] = loss(X[x1][y1], Y[x1][y1])

# Define a function to plot the loss in 3D
def plot_loss_3d():
    fig = plt.figure()
    ax = plt.axes(projection='3d')
    ax.set_title("Loss Function")
    ax.set_xlabel("a range")
    ax.set_ylabel("b range")
    ax.set_zlabel("Loss")
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='jet', edgecolor='none')

# Call the function to plot the loss
plot_loss_3d()

plt.show()
In [10]:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D



# Create a figure and a 3D Axes
fig = plt.figure(figsize=(12,10))
ax = Axes3D(fig)
ax.set_xlabel('Rooms', fontsize = 15)
ax.set_ylabel('Population', fontsize = 15)
ax.set_zlabel('Price', fontsize = 15)

plt.close()
In [11]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation

# Sample data
np.random.seed(0)
xs = np.random.rand(50, 2)
ys = xs[:, 0] + 2 * xs[:, 1] + np.random.rand(50)

# Initialize figure and 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Initialize parameters
Theta_0 = 0.0
Theta_1 = 0.0

def init():
    ax.scatter(xs[:, 0], xs[:, 1], ys, c='C6', marker='o', alpha=0.6)
    x0, x1 = np.meshgrid(xs[:, 0], xs[:, 1])
    yp = Theta_0 * x0 + Theta_1 * x1
    ax.plot_wireframe(x0, x1, yp, rcount=200, ccount=200, linewidth=0.5, color='C9', alpha=0.5)
    ax.legend(fontsize=15, labels=['Data points', 'Hyperplane'])
    return fig,

def animate(i):
    ax.view_init(elev=10., azim=i)
    return fig,

# Animate
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=360, interval=20, blit=True)
anim.save('animation.gif', writer='pillow', fps=30)
plt.close()

import io
import base64
from IPython.display import HTML

filename = 'animation.gif'


HTML(data='''<img src="data:image/gif;base64,{0}" type="gif" />'''.format(encoded.decode('ascii')))
Out[11]:
In [13]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Generate some sample 3D data for demonstration
np.random.seed(0)
X1 = 2 * np.random.rand(100, 1)
X2 = 3 * np.random.rand(100, 1)
y = 4 + 2 * X1 + 3 * X2 + np.random.rand(100, 1)

# Initialize the model parameters
theta0 = np.random.rand(1)
theta1 = np.random.rand(1)
theta2 = np.random.rand(1)

# Set hyperparameters
learning_rate = 0.01
num_iterations = 1000

# Lists to store the history of parameter values for plotting
theta0_history = []
theta1_history = []
theta2_history = []

# Perform gradient descent
for iteration in range(num_iterations):
    # Calculate the predicted values (hypothesis)
    y_pred = theta0 + theta1 * X1 + theta2 * X2

    # Calculate the gradients
    gradient_theta0 = np.sum(y_pred - y)
    gradient_theta1 = np.sum((y_pred - y) * X1)
    gradient_theta2 = np.sum((y_pred - y) * X2)

    # Update the model parameters using gradient descent
    theta0 = theta0 - learning_rate * gradient_theta0
    theta1 = theta1 - learning_rate * gradient_theta1
    theta2 = theta2 - learning_rate * gradient_theta2

    # Store the parameter values for plotting
    theta0_history.append(theta0[0])
    theta1_history.append(theta1[0])
    theta2_history.append(theta2[0])

# Print the final model parameters
print("Theta0 (Intercept):", theta0)
print("Theta1 (Slope for X1):", theta1)
print("Theta2 (Slope for X2):", theta2)

# Create a 3D scatter plot of the data points
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X1, X2, y, label='Data Points', c='b', marker='o')

# Create a meshgrid for the plane representing the linear regression model
x1_plane, x2_plane = np.meshgrid(np.linspace(X1.min(), X1.max(), 50), np.linspace(X2.min(), X2.max(), 50))
y_plane = theta0 + theta1 * x1_plane + theta2 * x2_plane

# Plot the linear regression plane
ax.plot_surface(x1_plane, x2_plane, y_plane, alpha=0.5, cmap='viridis')

ax.set_xlabel('X1')
ax.set_ylabel('X2')
ax.set_zlabel('y')
ax.set_title('3D Linear Regression using Gradient Descent')
plt.legend()
plt.grid(True)

# Plot the trajectory of the gradient descent in 3D
fig2 = plt.figure()
ax2 = fig2.add_subplot(111, projection='3d')
ax2.plot(theta0_history, theta1_history, theta2_history, label='Parameter Trajectory')
ax2.set_xlabel('Theta0')
ax2.set_ylabel('Theta1')
ax2.set_zlabel('Theta2')
ax2.set_title('Gradient Descent Trajectory in 3D')
plt.legend()
plt.grid(True)

plt.show()
C:\ProgramData\anaconda3\Lib\site-packages\numpy\core\fromnumeric.py:86: RuntimeWarning: overflow encountered in reduce
  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
C:\Users\hp\AppData\Local\Temp\ipykernel_7760\957577516.py:36: RuntimeWarning: invalid value encountered in subtract
  theta0 = theta0 - learning_rate * gradient_theta0
C:\Users\hp\AppData\Local\Temp\ipykernel_7760\957577516.py:37: RuntimeWarning: invalid value encountered in subtract
  theta1 = theta1 - learning_rate * gradient_theta1
C:\Users\hp\AppData\Local\Temp\ipykernel_7760\957577516.py:38: RuntimeWarning: invalid value encountered in subtract
  theta2 = theta2 - learning_rate * gradient_theta2
Theta0 (Intercept): [nan]
Theta1 (Slope for X1): [nan]
Theta2 (Slope for X2): [nan]
In [16]:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Generate some sample 3D data for demonstration
np.random.seed(0)
X1 = 2 * np.random.rand(100, 1)
X2 = 3 * np.random.rand(100, 1)
y = 4 + 2 * X1 + 3 * X2 + np.random.rand(100, 1)

# Initialize the model parameters
theta0 = np.random.rand(1)
theta1 = np.random.rand(1)
theta2 = np.random.rand(1)

# Set hyperparameters
learning_rate = 0.01
num_iterations = 1000

# Lists to store the history of parameter values for plotting
theta0_history = []
theta1_history = []
theta2_history = []
cost_history = []

# Perform gradient descent
for iteration in range(num_iterations):
    # Calculate the predicted values (hypothesis)
    y_pred = theta0 + theta1 * X1 + theta2 * X2

    # Calculate the gradients
    gradient_theta0 = np.sum(y_pred - y)
    gradient_theta1 = np.sum((y_pred - y) * X1)
    gradient_theta2 = np.sum((y_pred - y) * X2)

    # Update the model parameters using gradient descent
    theta0 = theta0 - learning_rate * gradient_theta0
    theta1 = theta1 - learning_rate * gradient_theta1
    theta2 = theta2 - learning_rate * gradient_theta2

    # Calculate the cost function (mean squared error)
    cost = np.mean((y_pred - y) ** 2)
    
    # Store the parameter values and cost for plotting
    theta0_history.append(theta0[0])
    theta1_history.append(theta1[0])
    theta2_history.append(theta2[0])
    cost_history.append(cost)

# Create a grid of parameter values for plotting the cost function
theta0_vals = np.linspace(-2, 6, 100)
theta1_vals = np.linspace(-2, 6, 100)
theta0_grid, theta1_grid = np.meshgrid(theta0_vals, theta1_vals)
cost_surface = np.zeros_like(theta0_grid)

for i in range(len(theta0_vals)):
    for j in range(len(theta1_vals)):
        cost_surface[i, j] = np.mean((y - (theta0_vals[i] + theta1_vals[j] * X1 + theta2 * X2))**2)

# Print the final model parameters
print("Theta0 (Intercept):", theta0)
print("Theta1 (Slope for X1):", theta1)
print("Theta2 (Slope for X2):", theta2)

# Create a 3D scatter plot of all data points
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X1, X2, y, label='Data Points', marker='o')

# Create a meshgrid for the plane representing the linear regression model
x1_plane, x2_plane = np.meshgrid(np.linspace(X1.min(), X1.max(), 50), np.linspace(X2.min(), X2.max(), 50))
y_plane = theta0 + theta1 * x1_plane + theta2 * x2_plane

# Plot the linear regression plane
ax.plot_surface(x1_plane, x2_plane, y_plane, alpha=0.5, cmap='viridis')

# Plot the trajectory of the gradient descent in parameter space
ax.plot(theta0_history, theta1_history, theta2_history, label='Parameter Trajectory', c='r', linewidth=2)

ax.set_xlabel('X1')
ax.set_ylabel('X2')
ax.set_zlabel('y')
ax.set_title('3D Linear Regression using Gradient Descent')
plt.legend()
plt.grid(True)

# Create a subplot for the cost function surface
fig2 = plt.figure(figsize=(10, 6))
ax2 = fig2.add_subplot(111, projection='3d')
ax2.plot_surface(theta0_grid, theta1_grid, cost_surface, cmap='viridis', alpha=0.7)
ax2.set_xlabel('Theta0')
ax2.set_ylabel('Theta1')
ax2.set_zlabel('Cost Function')
ax2.set_title('Cost Function Surface')
plt.grid(True)

plt.show()
C:\Users\hp\AppData\Local\Temp\ipykernel_7760\940254435.py:42: RuntimeWarning: overflow encountered in square
  cost = np.mean((y_pred - y) ** 2)
C:\Users\hp\AppData\Local\Temp\ipykernel_7760\940254435.py:37: RuntimeWarning: invalid value encountered in subtract
  theta0 = theta0 - learning_rate * gradient_theta0
C:\Users\hp\AppData\Local\Temp\ipykernel_7760\940254435.py:38: RuntimeWarning: invalid value encountered in subtract
  theta1 = theta1 - learning_rate * gradient_theta1
C:\Users\hp\AppData\Local\Temp\ipykernel_7760\940254435.py:39: RuntimeWarning: invalid value encountered in subtract
  theta2 = theta2 - learning_rate * gradient_theta2
Theta0 (Intercept): [nan]
Theta1 (Slope for X1): [nan]
Theta2 (Slope for X2): [nan]
In [ ]: